package com.ssoclient2.controller;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.ssoclient2.config.MySessionContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.*;
/**
 * The type Hello controller.
 * @author zchcpywin10
 */
@Controller
public class HelloController {
  /**
   * The Sso server url.
   */
  @Value("${sso.server.url}")
  String ssoServerUrl;
  /**
   * The Sso log out url.
   */
  @Value("${sso.server.logouturl}")
  String ssoLogOutUrl;
  private final Map<String, String> param = new HashMap<>();
  /**
   * 无需登录就可访问
   *
   * @return string
   */
  @ResponseBody
  @GetMapping("/hello")
  public String hello() {
    return "hello";
  }
  /**
   * Logout string.
   *
   * @return the string
   */
  @GetMapping("/logout111")
  public String logout() {
    return "redirect:" + ssoLogOutUrl;
  }
  /**
   * Logout clean.
   *
   * @param session   the session
   * @ param jessionId the jession id
   * @param sessionId the jession id
   */
  @GetMapping("/logoutClean")
  @ResponseBody
  // public void logoutClean(HttpSession session, @RequestParam("jessionId") String jessionId)
  public void logoutClean(HttpSession session, @RequestParam("sessionId") String sessionId) {
    // HttpSession session = session.getSessionContext().getSession(jessionId)
    Object loginUser = session.getAttribute("loginUser");
    HttpSession session1 = MySessionContext.getInstance().getSession(sessionId);
    Object loginUser1 = session1.getAttribute("loginUser");
    session1.invalidate();
    // session.invalidate();
  }
  /**
   * 感知这次是在 ssoserver 登录成功跳回来的。
   *
   * @param model   the model
   * @param session the session
   * @param token   只要去ssoserver登录成功跳回来就会带上
   * @return string
   */
  @GetMapping("/employees2")
  public String employees(Model model, HttpSession session, @RequestParam(value = "token", required = false) String token) {
    System.out.println("employees2 token = " + token);
    //
    if (!StringUtils.isEmpty(token)) {
      //去ssoserver登录成功跳回来就会带上
      HttpResponse response = HttpRequest.get("http://ssoserver.com:8083/checkToken?token=" + token).execute();
      String body1 = response.body();
      if ("true".equals(body1)) {
        //TODO 1、去ssoserver获取当前token真正对应的用户信息
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://ssoserver.com:8083/userInfo?token=" + token, String.class);
        String body = forEntity.getBody();
        // HttpSession session1 = session.getSessionContext().getSession(sessionId)
        session.setAttribute("loginUser", body);
        MySessionContext.getInstance().addSession(session);
      }
    }
    HttpSession session1 = MySessionContext.getInstance().getSession(session.getId());
    boolean equals = Objects.equals(session1, session);
    System.out.println("equals = " + equals);
    // MySessionContext.getInstance().getSession(session.getId())
    // Object loginUser = session1.getAttribute("loginUser")
    Object loginUser = session.getAttribute("loginUser");
    System.out.println("loginUser = " + loginUser);
    if (loginUser == null) {
      //没登录，跳转到登录服务器进行登录
      String sessionId = session.getId();
      //跳转过去以后，使用url上的查询参数标识我们自己是哪个页面
      //redirect_url=http://client1.com:8080/employees
      return "redirect:" + ssoServerUrl + "?redirect_url=http://client2.com:8082/employees2&url=http://client1.com:8082/logoutClean&jsessionId=" + sessionId;
    }
    else {
      List<String> emps = new ArrayList<>();
      emps.add("张三");
      emps.add("李四");
      model.addAttribute("emps", emps);
      return "list";
    }
  }
}
